// @flow
import React from 'react'
import Timeline from 'components/Timeline'
import ResumeGrid from 'components/ResumeGrid'
import universalFetch, { handleFetchError } from 'store/modules/fetch'
import ResumeTab from 'components/ResumeTab'
import { connect } from 'react-redux'
import moment from 'moment'
type Props = {
  deviceId: number,
  history: Object
}
type States = {
  gridData: Array<Object>,
  types: Array<Object>
}
const colors = ['#79D550', '#F06D73', '#E6C43D', '#D08BE9', '#9ACDDE', '#BBA182']
class Resume extends React.Component {
  props: Props
  state: States
  getTypes: Function
  constructor (props: Props) {
    super(props)
    this.state = {
      gridData: [],
      types: []
    }
    this.getTypes = this.getTypes.bind(this)
  }
  componentDidMount () {
    const { deviceId } = this.props
    const component = this
    // 所有事件
    universalFetch(`${__SERVICE_API__}device/devices/${deviceId}/events?orderBy=start_time desc`)
      .then((res) => res.json())
      .then((json) => {
        if (json.code !== 200000) {
          throw new Error(JSON.stringify({ code: json.code, message: json.message }))
        }
        component.setState({
          gridData: json.result.eventList.map((d, i) => {
            d['startTime'] = moment(d.startTime, 'YYYY-MM-DDTHH:mm:ss.SSSZ').format('YYYY-MM-DD HH:mm:ss')
            d['endTime'] = moment(d.endTime, 'YYYY-MM-DDTHH:mm:ss.SSSZ').format('YYYY-MM-DD HH:mm:ss')
            return d
          }),
          types: json.result.types.map((d, i) => {
            d['color'] = colors[i]
            d['active'] = true
            return d
          })
        })
      })
      .catch((error) => {
        handleFetchError(error)
        console.error(error)
      })
  }
  getTypes (types) {
    const { deviceId } = this.props
    const component = this
    if (types.length === 0) {
      component.setState({
        gridData: []
      })
    } else {
      universalFetch(`${__SERVICE_API__}device/devices/${deviceId}/events?types=${types}&orderBy=start_time desc`)
        .then((res) => res.json())
        .then((json) => {
          if (json.code !== 200000) {
            throw new Error(JSON.stringify({ code: json.code, message: json.message }))
          }
          component.setState({
            gridData: json.result.eventList
          })
        })
        .catch((error) => {
          handleFetchError(error)
          console.error(error)
        })
    }
  }
  render () {
    const { gridData, types } = this.state
    return (
      <div style={{ overflow: 'hidden', padding: '20px', height: '100%' }}>
        <Timeline timeData={gridData} history={this.props.history} />
        <ResumeTab types={types} onChange={this.getTypes} />
        <ResumeGrid {...{
          history: this.props.history,
          rowData: gridData
        }} />
      </div>
    )
  }
}
const mapStatesToProps = (state) => {
  return {
    deviceId: state.deviceCard.deviceData.id
  }
}
export default connect(mapStatesToProps, {})(Resume)
